lobby = {}
lobby.map = {}      --Holds number of players in a level.
lobby.voted = {}  --Holds player names, and if they have voted.
lobby.votes = {}
lobby.suspect = {} --Holds number of votes a player has marking them as suspect
lobby.game = {}     --Holds player names and what map they are on.
lobby.xp = {}       --The current XP amount for each level.
lobby.traitors = {} --
lobby.corpses = {}
lobby.vote_timer = {}
--Yes I know these table names are not very clear.

--[[
Saving data:
local data = {}
data.level_pos = {x = pos_x, y = pos_y, z = pos_z}
data.xp = tonumber(fields.xp)
lobby.savedata.IDs[map_id] = true
lobby.savedata.data[map_id] = data

----
Retriving data:
local game_data = lobby.savedata.data[map_id]
local game_pos = game_data['level_pos']
]]

dofile(minetest.get_modpath('lobby')..'/chat.lua')
dofile(minetest.get_modpath('lobby')..'/corpse.lua')
dofile(minetest.get_modpath('lobby')..'/functions.lua')
dofile(minetest.get_modpath('lobby')..'/nodes_active.lua')
dofile(minetest.get_modpath('lobby')..'/tool.lua')

local function not_builder(player)
   local name = player:get_player_name()
   return not minetest.check_player_privs(name, { creative = true })
end

minetest.register_on_joinplayer(function(player)
   local name = player:get_player_name()
   lobby.suspect[name] = 0
   lobby.game[name] = 'lobby'
   lobby.voted[name] = true
   player:set_hp(40)
   player:set_attribute('ghost', 'false')
   if not_builder(player) then
      local player_inv = player:get_inventory()
      player_inv:set_list('main', {})
      local game_pos = {x=0, y=0, z=0}
      player:setpos({x=game_pos.x+(math.random(-2,2)),y=game_pos.y,z=game_pos.z+(math.random(-2,2))})
   end
end)

minetest.register_on_dieplayer(function(player)
   local name = player:get_player_name()
   local map_id = lobby.game[name]
   player:set_physics_override({speed=1})
   local player_inv = player:get_inventory()
   player_inv:set_list('main', {})
   if map_id ~= 'lobby' then
      local traitor = lobby.traitors[map_id]
      local pos = vector.round(player:get_pos())
      local param2 = minetest.dir_to_facedir(player:get_look_dir())
      local pos_node = minetest.get_node(pos)
      if pos_node.name ~= 'air' then
         local air = minetest.find_node_near(pos, 1, {"air"})
         minetest.set_node(air, {name = "lobby:corpse", param2 = param2})
         lobby.corpse_entry(air, map_id)
      else
         minetest.set_node(pos, {name = "lobby:corpse", param2 = param2})
         lobby.corpse_entry(pos, map_id)
      end
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', name..'\'s Corpse.')
      gamer.player_set_textures(player,{'lobby_ghost.png'})
      player:set_nametag_attributes({
         color = {a = 0, r = 255, g = 255, b = 255}
      })
      player:set_properties({visual_size = {x = 0, y = 0}, collisionbox = {-0.3, 0.0, -0.3, 0.3, .2, 0.3}})
      player:set_attribute('ghost', 'true')
      minetest.chat_send_player(name, 'You\'re now a ghost. Return to the lobby with the /lobby chat command.')
      lobby.game[name] = 'lobby'
      lobby.update_maps(map_id)
      if name == traitor then
         lobby.team_win(map_id)
      end
   end
end)

minetest.register_on_leaveplayer(function(player, timed_out)
   local name = player:get_player_name()
   local map = lobby.game[name]
   if map ~= 'lobby' then
      local traitor = lobby.traitors[map]
      minetest.chat_send_all('somebody quit the '..map..' level')
      local player_inv = player:get_inventory()
      player_inv:set_list('main', {})
      lobby.update_maps(map)
      if traitor == name then
         lobby.team_win(map)
      end
   end
end)

lobby.load()
minetest.register_on_shutdown(lobby.save)
